﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CurriculumnManage.Helper;
using CurriculumnManage.DBAccess;
using System.Configuration;
using System.Data.SqlClient;

namespace CurriculumnManage
{
    public partial class frmDownFromGakuseki : Form
    {
        public frmDownFromGakuseki()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (!CheckInput()) return;
                this.Button1.Enabled = false;
                this.Button2.Enabled = false;
                DownLoadData();
                CommonFunction.ShowMsg("学績からのデータインポートは完了しました。");
                this.Button1.Enabled = true;
                this.Button2.Enabled = true;
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("downData", ex.Message);
                this.Button1.Enabled = true;
                this.Button2.Enabled = true;
            }
        }

        private bool CheckInput()
        {
            if (!CommonFunction.CheckYear(this.txtYear))
            {
                return false;
            }
            if(!CommonFunction.CheckGrade(this.cmbGrade))
            {
                return false;
            }
            return true;
        }

        private void DownLoadData()
        {
            string targetDBConn = ConfigurationManager.ConnectionStrings["CurriculumnManageDB"].ConnectionString;
            string sourceDBConn = ConfigurationManager.ConnectionStrings["STR0100DB"].ConnectionString;
            int iNendo = MyUtils.ParseToInt(this.txtYear.Text, 0);
            int? iGrade = MyUtils.ParseToInt(this.cmbGrade.Text, 0);
            if (iGrade == 0) iGrade = null;
            DBContext targetDB = new DBContext(targetDBConn);
            DBContext sourceDB = new DBContext(sourceDBConn);
            try
            {
                targetDB.BeginTrans();
                MyLog.TraceInstance.TraceInformation("Delete all the data start:");
                targetDB.CallProcedure("usp_delete_all_data_inWebCurriCulumn", iNendo, iGrade);
                MyLog.TraceInstance.TraceInformation("Delete all the data OK");

                MyLog.TraceInstance.TraceInformation("Import STRTeacher start");
                CommonFunction.CopyData(targetDB, sourceDB, "usp_webCurriCulumn_tbSTRTeacher_get", "tbSTRTeacher", "usp_tbstrTeacher_add_edit");
                MyLog.TraceInstance.TraceInformation("Import STRTeacher OK");

                MyLog.TraceInstance.TraceInformation("Import tbsTRTeacherDepartmen start");
                CommonFunction.CopyData(targetDB, sourceDB, "usp_webCurriCulumn_tbsTRTeacherDepartment_get", "tbstrTeacherDepartment", "usp_tbstrTeacherDepartment_add_edit");
                MyLog.TraceInstance.TraceInformation("Import tbsTRTeacherDepartmen OK");

                MyLog.TraceInstance.TraceInformation("Import tbSTRMapTeacherDepartment start");
                CommonFunction.CopyData(targetDB, sourceDB, "usp_webCurriCulumn_tbSTRMapTeacherDepartment_get", "tbSTRMapTeacherDepartment", "usp_tbSTRMapTeacherDepartment_add_edit");
                MyLog.TraceInstance.TraceInformation("Import tbSTRMapTeacherDepartment OK");

                MyLog.TraceInstance.TraceInformation("Import tbSTRSubjectGradeProfile start");
                CommonFunction.CopyData(targetDB, sourceDB, "usp_webCurriCulumn_tbSTRSubjectGradeProfile_get", "tbSTRSubjectGradeProfile", "usp_tbSTRSubjectGradeProfile_add_edit", null, iNendo, iGrade == 0 ? null : iGrade);
                MyLog.TraceInstance.TraceInformation("Import tbSTRSubjectGradeProfile OK");

                MyLog.TraceInstance.TraceInformation("Import tbSTRSubjectProfile start");
                CommonFunction.CopyData(targetDB, sourceDB, "usp_webCurriCulumn_tbSTRSubjectProfile_get", "tbSTRSubjectProfile", "usp_tbSTRSubjectProfile_add_edit");
                MyLog.TraceInstance.TraceInformation("Import tbSTRSubjectProfile OK");

                MyLog.TraceInstance.TraceInformation("Import tbSTRSubjectScheduleProfile start");
                CommonFunction.CopyData(targetDB, sourceDB, "usp_webCurriCulumn_tbSTRSubjectScheduleProfile_get", "tbSTRSubjectScheduleProfile", "usp_tbSTRSubjectScheduleProfile_add_edit",iNendo,iGrade);
                MyLog.TraceInstance.TraceInformation("Import tbSTRSubjectScheduleProfile OK");

                MyLog.TraceInstance.TraceInformation("Import tbSTRPeriodProfile start");
                CommonFunction.CopyData(targetDB, sourceDB, "usp_webCurriCulumn_tbSTRPeriodProfile_get", "tbSTRPeriodProfile", "usp_tbSTRPeriodProfile_add_edit");
                MyLog.TraceInstance.TraceInformation("Import tbSTRPeriodProfile OK");

                MyLog.TraceInstance.TraceInformation("Import tbSTRClassType start");
                CommonFunction.CopyData(targetDB, sourceDB, "usp_webCurriCulumn_tbSTRClassType_get", "tbSTRClassType", "usp_tbSTRClassType_add_edit");
                MyLog.TraceInstance.TraceInformation("Import tbSTRClassType OK");

                MyLog.TraceInstance.TraceInformation("Import tbSTRLectureStatus start");
                CommonFunction.CopyData(targetDB, sourceDB, "usp_webCurriCulumn_tbSTRLectureStatus_get", "tbSTRLectureStatus", "usp_tbSTRLectureStatus_add_edit");
                MyLog.TraceInstance.TraceInformation("Import tbSTRLectureStatus OK");

                //MyLog.TraceInstance.TraceInformation("Import tbSTRLectureTeacher start");
                //CommonFunction.CopyData(targetDB, sourceDB, "usp_webCurriCulumn_tbSTRLectureTeacher_get", "tbSTRLectureTeacher", "usp_tbSTRLectureTeacher_add_edit",iNendo,iGrade);
                //MyLog.TraceInstance.TraceInformation("Import tbSTRLectureTeacher OK");

                MyLog.TraceInstance.TraceInformation("Import tbSTRClass start");
                CommonFunction.CopyData(targetDB, sourceDB, "usp_webCurriCulumn_tbSTRClass_get", "tbSTRClass", "usp_tbSTRClass_add_fromStr0100", iNendo, iGrade);
                MyLog.TraceInstance.TraceInformation("Import tbSTRClass OK");



                targetDB.Commit();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                targetDB.RollBack();
                targetDB.Close();
                sourceDB.Close();
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmDownFromGakuseki_Load(object sender, EventArgs e)
        {
            CommonFunction.SetDefaultYear(this.txtYear);
        }


    }
}
